//
// Created by xili on 2024/8/21 16:54.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution1 {
public:
    vector<int> findDuplicates(vector<int> &nums) {
        unordered_set<int> us;
        vector<int> ans;
        for (auto &item: nums) {
            if (us.count(item)) {
                ans.push_back(item);
            } else {
                us.insert(item);
            }
        }
        return ans;
    }
};

//正负号标记
class Solution2 {
public:
    vector<int> findDuplicates(vector<int> &nums) {
        int n = nums.size();
        vector<int> ans;
        for (int i = 0; i < n; i++) {
            int x = abs(nums[i]);
            if (nums[x - 1] > 0) {
                nums[x - 1] = -nums[x - 1];
            } else {
                ans.push_back(x);
            }
        }
        return ans;
    }
};